<style lang="scss" scoped>
.map-container {
  background-color: rgb(240, 242, 245);
  height: 100%;
  .el-header {
    padding: 0;
  }
  .el-main {
    padding: 0;
    position: relative;
  }
}
.map {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  position: relative;
}
.tool-bar {
  $h: 40px;
  height: $h;
  width: 100%;
  background-color: white;
  .title {
    display: inline-block;
    line-height: $h;
    height: $h;
    padding-left: 20px;
    font-weight: bold;
  }
  ul {
    padding: 0;
    margin: 0;
    list-style: none;
    float: right;
    position: relative;
    &:after {
      content: "";
      position: absolute;
      width: 1px;
      height: 18px;
      top: 11px;
      left: 0;
      z-index: 10000;
      background-color: #000;
    }
    &:last-child:after {
      display: none;
    }
    li {
      float: left;
      height: 30px;
      line-height: 30px;
      width: 30px;
      margin-top: 5px;
      text-align: center;
      position: relative;
      &:hover {
        background-color: #000;
        color: #fff;
        span {
          display: inline;
          color: #000000;
        }
      }
      span {
        font-size: 12px;
        position: absolute;
        top: 35px;
        left: 0;
        z-index: 1000001;
        padding: 0 4px;
        line-height: 25px;
        height: 25px;
        border: 1px solid #eee;
        white-space: nowrap;
        background-color: #fff;
        display: none;
      }
    }
  }
}
</style>
<template>
  <div>
    <el-container class="map-container">
      <el-header height="40px">
        <div class="tool-bar">
          <div class="title">湖南省</div>
          <ul>
            <li>
              <i class="el-icon-magic-stick"></i>
              <span>测量</span>
            </li>
          </ul>
          <ul>
            <li>
              <i class="el-icon-edit"></i>
              <span>标绘</span>
            </li>
            <li>
              <i
                @click="clearDraw"
                class="el-icon-refresh-left"
              ></i>
              <span>清除标绘</span>
            </li>
          </ul>
          <ul>
            <li>
              <i class="el-icon-warning-outline"></i>
              <span>信息</span>
            </li>
          </ul>
        </div>
      </el-header>
      <el-main class="main">
        <div
          class="map"
          ref="map"
          :style="{'padding-bottom':mapPadding}"
        >
        </div>
        <drawPanel
          v-bind:sk="skViewModel"
          v-bind:drawLayer="drawLayer"
        ></drawPanel>
        <layer-tree
          ref="tree"
          v-bind:map="map"
        ></layer-tree>

      </el-main>
    </el-container>
  </div>
</template>

<script>
import { loadModules } from 'esri-loader';
import drawPanel from "@/components/DrawPanel";
import layerTree from "@/components/LayerTree";
export default {
  data () {
    return {
      mapPadding: "",
      view: {},
      map: {},
      skViewModel: {},
      drawLayer: {},
    }
  },
  components: {
    drawPanel,
    layerTree
  },
  mounted () {
    let sketchViewModel = null;
    loadModules(
      [
        "esri/Map",
        "esri/views/MapView",
        "esri/layers/TileLayer",
        "esri/layers/MapImageLayer",
        "esri/Basemap",
        "esri/Graphic",
        "esri/layers/GraphicsLayer",
        "esri/tasks/support/FeatureSet",
        "esri/symbols/SimpleLineSymbol",
        "esri/widgets/BasemapToggle",
        "esri/widgets/ScaleBar",
        "esri/widgets/Search",
        "esri/widgets/Zoom",
        "esri/widgets/LayerList",
        "esri/widgets/Sketch/SketchViewModel"
      ],
      {
        css: false,
        url:
          "http://www.dzmap.cn/webapps/other/arcgis/arcgis_js_api/library/4.7/init.js"
      }
    ).then(
      ([
        ArcGisMap,
        MapView,
        TileLayer,
        MapImageLayer,
        Basemap,
        Graphic,
        GraphicsLayer,
        FeatureSet,
        SimpleLineSymbol,
        BasemapToggle,
        ScaleBar,
        Search,
        Zoom,
        LayerList,
        SketchViewModel
      ]) => {
        //基础图层
        var veclayer = new TileLayer({
          url: "http://www.dzmap.cn/arcgis/rest/services/vec/MapServer",
          title: "矢量地图"
        });
        var cvalayer = new TileLayer({
          url: "http://www.dzmap.cn/arcgis/rest/services/ant/MapServer",
          title: "矢量注记地图"
        });
        var cialayer = new TileLayer({
          url: "http://www.dzmap.cn/arcgis/rest/services/imgzhuji/MapServer",
          title: "影像注记地图"
        });
        var imglayer = new TileLayer({
          url:
            "http://www.dzmap.cn/arcgis/rest/services/img_services/MapServer",
          title: "影像地图"
        });

        var vecBasemap = new Basemap({
          baseLayers: [veclayer, cvalayer],
          title: "矢量地图"
          // thumbnailUrl: "http://10.43.173.222:8012/public/images/vec.png",
        });

        var basemap = new Basemap({
          baseLayers: [imglayer, cialayer],
          // thumbnailUrl: "http://10.43.173.222:8012/public/images/img.png",
          title: "影像地图"
        });
        this.map = new ArcGisMap({
          basemap: basemap
        });
        this.view = new MapView({
          container: this.$refs.map, // Reference to the scene div created in step 5
          map: this.map, // Reference to the map object created before the scene
          center: [113, 29],
          zoom: 2
        });
        var drawLayer = new GraphicsLayer({
          title: "标绘"
        });
        this.drawLayer = drawLayer;
        this.map.add(drawLayer);
        let view = this.view;
        let _this = this;
        view
          .when(function () {
            var toggle = new BasemapToggle({
              view: view,
              nextBasemap: vecBasemap
            });
            var scaleBar = new ScaleBar({
              view: view
            });
            var zoom = new Zoom({
              view: view
            });

            view.ui.add(toggle, "bottom-right");
            view.ui.add(scaleBar, {
              position: "bottom-left"
            });
            view.ui._removeComponents(["attribution"]);
            var polylineSymbol = {
              // symbol used for polylines
              type: "simple-line", // autocasts as new SimpleMarkerSymbol()
              color: "rgba(77,99,144,1)",
              width: "4",
            };
            var polygonSymbol = {
              // symbol used for polygons
              type: "simple-fill", // autocasts as new SimpleMarkerSymbol()
              color: "rgba(77,99,144, 0.8)",
              style: "solid",
              outline: {
                color: "white",
                width: 1
              }
            };
            var updateGraphic = null;
            sketchViewModel = new SketchViewModel({
              view: view,
              layer: drawLayer,
              polylineSymbol: polylineSymbol,
              polygonSymbol: polygonSymbol
            });
            sketchViewModel.on("draw-start", function () {
              _this.drawState = true
            });
            sketchViewModel.on("draw-complete", addGraphic);

            function addGraphic (evt) {
              var geometry = evt.geometry;
              var symbol;

              // Choose a valid symbol based on return geometry
              switch (geometry.type) {
                case "point":
                  symbol = pointSymbol;
                  break;
                case "polyline":
                  symbol = polylineSymbol;
                  break;
                default:
                  symbol = polygonSymbol;
                  break;
              }
              // Create a new graphic; add it to the GraphicsLayer
              var graphic = new Graphic({
                geometry: geometry,
                symbol: symbol
              });
              drawLayer.add(graphic);
              // Remove stored reference to the updated graphic
              // Required in 'update-complete' callback specifically
              updateGraphic = null;
              _this.drawState = false;
            }
            _this.skViewModel = sketchViewModel;
            // _this.setUpClickHandler();
            _this.$refs.tree.init()
          })
          .otherwise(function (err) {
            console.log(err);
          });
      }
    )
    let nodes = this.$refs.tree.nodes
    this.selectLayerList = nodes;
  },

  methods: {
    setUpClickHandler () {
      let view = this.view;
      let sketchViewModel = this.skViewModel;
      let tempGraphicsLayer = this.drawLayer;
      view.on("click", function (evt) {
        view.hitTest(evt).then(function (response) {
          var results = response.results;
          // Found a valid graphic
          if (results.length && results[results.length - 1].graphic) {
            // Check if we're already editing a graphic
            if (!updateGraphic) {
              // Save a reference to the graphic we intend to update
              updateGraphic = results[results.length - 1].graphic;
              // Remove the graphic from the GraphicsLayer
              // Sketch will handle displaying the graphic while being updated
              drawLayer.remove(updateGraphic);
              sketchViewModel.update(updateGraphic.geometry);
            }
          }
        });
      });
    },
    changeMapPadding (height) {
      this.mapPadding = height + "px";
    },
    clearDraw () {
      this.drawLayer.removeAll()
    },
  }
}
</script>